<template>
  <div class="login-page">
    <van-nav-bar title="账号登录" left-arrow @click-left="logFn"/>
    <van-form @submit="onSubmit">
      <van-field 
        v-model="username"
        name="username"

        placeholder="请输入账号"
        :rules="[
          { required: true, message: '请填写用户名' },
          { pattern: /^\w{5,}$/, message: '用户名至少包含5位字符' },
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"

        placeholder="请输入密码"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^\w{6,}$/, message: '密码至少包含6位字符' },
        ]"
      />
      <div style="margin: 16px">
        <van-button block style="backgroundColor:#1cb676" type="primary" native-type="submit">登录</van-button>
      </div>
      <router-link class="link" to="/register">还没有账号，去注册~</router-link>
    </van-form>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'

export default {
  name: 'login-page',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async  onSubmit (val) {
      // console.log('onSubmit执行了', val)
      const  data  = await login(val)
      // console.log(data)
      setToken(data.body.token)
      this.$toast.success(data.description)
      this.$router.push('/index')
    },
    logFn(){
      this.$router.back()
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  display: block;
  color: #333;
  font-size: 12px;
  padding-left: 130px;
  margin-top: 15px;
  float: left;
}
.van-field {
  line-height: 50px;
}
</style>
